<?php
/**
 * Page Template
 *
 * @package templateSystem
 * @copyright Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: tpl_product_reviews_default.php 4852 2006-10-28 06:47:45Z drbyte $
 */
?>
<div class="centerColumn" id="reviewsDefault">
<?php
  if (zen_not_null($products_image)) {
  /**
   * require the image display code
   */
      
    $manufacturers_name= zen_get_products_manufacturers_name((int)$_GET['products_id']);
    $flag_show_product_info_manufacturer = zen_get_show_product_switch($_GET['products_id'], 'manufacturer');
?>
<h1><span style="color: black"><?php echo REVIEW_SECTION_TITLE . ' <a href="' . zen_href_link(zen_get_info_page($_GET['products_id']), zen_get_all_get_params(array('reviews_id'))) . '">'  . $products_name .  '</a>'; ?>
    <?php echo (($flag_show_product_info_manufacturer == 1 and !empty($manufacturers_name)) ? '&nbsp;(<a href="' . zen_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $manufacturers_id) . '">' . $manufacturers_name . '</a>)' : '');   ?>
</span></h1>
<br />
<div id="productReviewsDefaultProductImage" class="centeredContent back"><?php require($template->get_template_dir('/tpl_modules_main_product_image.php',DIR_WS_TEMPLATE, $current_page_base,'templates'). '/tpl_modules_main_product_image.php'); ?></div>
<?php
  }
?>

<div>
            
        <?php
  if ($flag_show_product_info_reviews == 1) {    
    // if more than 0 reviews, then show reviews button; otherwise, show the "write review" button
    if ($reviews->fields['count'] > 0 ) { ?>
<?php
         if ($flag_show_product_info_reviews_count == 1) {
    ?>
	<div class="forward centeredContent">
	<p> </p>
    <div class="biggerText"><strong><?php echo REVIEW_USEFUL5; ?></strong><br />
    <?php
           $stars_image_suffix = str_replace('.', '_', zen_round($reviews_average_rating->fields['average_rating'] * 2, 0) / 2); // for stars_0_5.gif, stars_1.gif, stars_1_5.gif etc.
           $average_rating = zen_round($reviews_average_rating->fields['average_rating'], 2);
           echo  zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $stars_image_suffix . '.gif', sprintf(BOX_REVIEWS_TEXT_OF_5_STARS, $average_rating));
           if((int)$reviews->fields['count'] === 1)
               echo '<span style="vertical-align: 7px"> ' . TEXT_PRODUCTS_INFO_ONE_REVIEW_1 . '</span>';
           else
               echo '<span style="vertical-align: 7px"> ' . sprintf(TEXT_PRODUCTS_INFO_MULTIPLE_REVIEWS_1, $reviews->fields['count']) . '</span>';           
    ?>
    </div><br />
    <div><?php echo REVIEW_USEFUL6; ?><br /><br />
    
        <?php echo '<a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, zen_get_all_get_params(array('reviews_id'))) . '">' . zen_image_button(BUTTON_IMAGE_WRITE_REVIEW, BUTTON_WRITE_REVIEW_ALT) . '</a>'; ?>
    
	</div></div>
    <?php
         } 
       ?><?php } 
    else { 
        echo '<div class="button"><a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, zen_get_all_get_params()) . '">' . TEXT_PRODUCTS_INFO_NO_REVIEW . '</a></div>';
    }
  }
?>
    </div>
</div>
<div class="forward">
<div class="buttonRow">
<?php
        // more info in place of buy now
        if (zen_has_product_attributes($review->fields['products_id'] )) {
          //   $link = '<p>' . '<a href="' . zen_href_link(zen_get_info_page($review->fields['products_id']), 'products_id=' . $review->fields['products_id'] ) . '">' . MORE_INFO_TEXT . '</a>' . '</p>';
          $link = '';
        } else {
          $link= '<a href="' . zen_href_link($_GET['main_page'], zen_get_all_get_params(array('action', 'reviews_id')) . 'action=buy_now') . '">' . zen_image_button(BUTTON_IMAGE_IN_CART, BUTTON_IN_CART_ALT) . '</a>';
        }
        //$the_button = $link;
        //$products_link = '';
        //echo zen_get_buy_now_button($review->fields['products_id'], $the_button, $products_link) . '<br />' . zen_get_products_quantity_min_units_display($review->fields['products_id']);
      ?>
</div>
</div>

<br class="clearBoth" />

<?php
  if ($reviews_split->number_of_rows > 0) {
    if ((PREV_NEXT_BAR_LOCATION == '1') || (PREV_NEXT_BAR_LOCATION == '3') && 1==0) {//never use paging
?>

<div id="productReviewsDefaultListingTopNumber" class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>

<div id="productReviewsDefaultListingTopLinks" class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>

<?php
    }
    //Vu - remove and change if state
    if(sizeof($reviewsArray) > 0){  
        $list = listReviews($db, '', $reviewsArray);
        echo '<div id="productReviews">' . $list . '</div>';
    }
    //Vu - end edit
?>
<?php
  } else {
?>
<?php
  }

  if (($reviews_split->number_of_rows > 0) && ((PREV_NEXT_BAR_LOCATION == '2') || (PREV_NEXT_BAR_LOCATION == '3'))) {
?>
<hr />
<div id="productReviewsDefaultListingBottomNumber" class="navSplitPagesResult"><?php echo $reviews_split->display_count(TEXT_DISPLAY_NUMBER_OF_REVIEWS); ?></div>
<div id="productReviewsDefaultListingBottomLinks" class="navSplitPagesLinks"><?php echo TEXT_RESULT_PAGE . ' ' . $reviews_split->display_links(MAX_DISPLAY_PAGE_LINKS, zen_get_all_get_params(array('page', 'info', 'main_page'))); ?></div>

<?php
  }
?>
    <div class="buttonRow centeredContent"><?php echo '<a href="' . zen_href_link(zen_get_info_page($_GET['products_id']), zen_get_all_get_params(array('reviews_id'))) . '">' . zen_image_button(BUTTON_IMAGE_GOTO_PROD_DETAILS , BUTTON_GOTO_PROD_DETAILS_ALT) . '</a> <a href="' . zen_href_link(FILENAME_PRODUCT_REVIEWS_WRITE, zen_get_all_get_params(array('reviews_id'))) . '">' . zen_image_button(BUTTON_IMAGE_WRITE_REVIEW, BUTTON_WRITE_REVIEW_ALT) . '</a>'; ?></div>

</div>
<form method="POST" action="<?php echo zen_href_link(FILENAME_PRODUCT_REVIEWS, zen_get_all_get_params(),'NONSSL') ?>" name="reviews_yes_no">
    <input type="hidden" value="" id="hd_review_yes_no" name="reviews_yes_no" />        
    <input type="hidden" value="" id="hd_review_yes_no_id" name="reviews_yes_no_id" />        
</form>
<?php
//Vu - add this function
function listReviews($db, $parentid, $reviewsArray, $res = '', $level = ''){
    foreach($reviewsArray as $reviews){
        if($reviews['reviewParentId'] == $parentid){
            $re = "<div class='child{$level}'><p>" ;
            if($parentid == NULL){
                $re .= $reviews["reviewsYes"] . " " . REVIEW_USEFUL . " " . $reviews["reviewsVotes"]. " " . REVIEW_USEFUL2 . "<br />" 
                    . zen_image(DIR_WS_TEMPLATE_IMAGES . 'stars_' . $reviews['reviewsRating'] . '.gif') ;
            
                $re .= "<span class='biggerText' style='vertical-align: 7px'><strong><a href='" . zen_href_link(FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . (int)$_GET['products_id'] . '&reviews_id=' . $reviews['id']) . "'>" . $reviews["reviewsTitle"] . "</a></strong></span>"
                    . "<span style='vertical-align: 7px'> - <em>" . zen_date_short($reviews['dateAdded']) . "</em></span><br />"
                    . sprintf(TEXT_REVIEW_BY, $reviews['customersName'])."</p>";
            }
            $re .=   "<p class='biggerText' >" .  $reviews['reviewsText'] . "</p>";
            if($parentid == NULL){
                $re .= $reviews["owner"]; 
            }
            $re .= "<p>". REVIEW_USEFUL3;
            $reviews_votes_sql = "SELECT votes_id FROM votes WHERE reviews_id = :reviewsID AND customers_id = :customerID";
            $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':reviewsID', $reviews['id'], 'integer');
            $reviews_votes_sql = $db->bindVars($reviews_votes_sql, ':customerID', $_SESSION['customer_id'], 'integer');
            $reviews_votes_result = $db->Execute($reviews_votes_sql);

            if($reviews_votes_result->EOF){
                  $re .= ' <input type="button" onclick="reviews(1,' . $reviews["id"] . ')" value="' 
                          . REVIEW_YES_BUTTON . '" style="width: 30px" /> <input type="button" onclick="reviews(0,' . $reviews["id"] . ')" value="' 
                          . REVIEW_NO_BUTTON . '" style="width: 30px" /> '; 
            }
            else{

                $reviews_votes_countyes_sql = "SELECT count(votes_id) as countyes FROM votes WHERE reviews_id = :reviewsID AND yes_no_vote = 1";
                $reviews_votes_countyes_sql = $db->bindVars($reviews_votes_countyes_sql, ':reviewsID', $reviews['id'], 'integer');
                $reviews_votes_countyes_result = $db->Execute($reviews_votes_countyes_sql);

                $reviews_votes_countno_sql = "SELECT count(votes_id) as countno FROM votes WHERE reviews_id = :reviewsID AND yes_no_vote = 0";
                $reviews_votes_countno_sql = $db->bindVars($reviews_votes_countno_sql, ':reviewsID', $reviews['id'], 'integer');
                $reviews_votes_countno_result = $db->Execute($reviews_votes_countno_sql);

               $re .= " " . "VOTES: " . ($reviews_votes_countyes_result->fields['countyes'] - $reviews_votes_countno_result->fields['countno']). " " . ALREADY_VOTES_THIS_REVIEW;
            }

            $re .= REVIEW_USEFUL3;
            $re .= "<input type='button' id='{$reviews["id"]}' class='comment-button' value='Comment'/>";
            $re .= "</p></div>";
            $res .= listReviews($db, $reviews['id'], $reviewsArray, $re, $level.'1');
        }
    }
    return $res;
}
//Vu - end edit
?>
<script type="text/javascript">
    function reviews(yesno, rid)
    {
        $('#hd_review_yes_no').val(yesno);
        $('#hd_review_yes_no_id').val(rid);
        $('[name=reviews_yes_no]').submit();
    }
    
</script>
